<template>
  <div>
    <el-row>
      <el-col :span="5"></el-col>
      <el-col :span="15" style="margin-top: 5vh">
        <el-row :gutter="100">
          <el-col :span="18" >
            <el-input @keyup.enter="handleSearch(page.search)" v-model="page.search" placeholder="输入 内容+回车 搜索">
              <template #prefix>
                <el-icon class="el-input__icon"><search /></el-icon>
              </template>
              <template #append>
                <el-button @click="handleSearch(page.search)">
                  <el-icon><ArrowRightBold /></el-icon>
                </el-button>
              </template>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addNotice()" >发布</el-button >
          </el-col>
        </el-row>
        <el-table :data="userList" style="width: 100%; margin-top: 2vh;">
          <el-table-column type="index" :index="index"/>
          <el-table-column prop="order" label="imp" width="60" />
          <el-table-column prop="content" label="content" width="500" />
          <el-table-column prop="updateTime" label="updateTime"/>
          <el-table-column label="操作" min-width="180" header-align="center" align="center">
            <template #default="props">
              <el-button type="warning" @click="setUser(props.row.id, props.row.content, props.row.order)" >修改</el-button >
              <el-button type="danger" @click="deleteUser(props.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="margin-left: 20vw; margin-top: 2vh;"
          :currentPage="page.currentPage"
          :page-size="page.pageSize"
          layout="prev, pager, next"
          :total="page.num"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { ArrowRightBold, Search } from '@element-plus/icons-vue'
import api from '../../api/data.js'
import { ElMessage } from 'element-plus'
export default {
  name: 'AdminNotice',
  components: {
    ArrowRightBold,
    Search
  },
  data () {
    return {
      userList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        num: 1,
        search: ''
      }
  }},
  methods: {
    addNotice () {
      this.$router.push({path: '/admin/addNotice'})
    },
    setUser (id, content, order) {
      this.$router.push({path: '/admin/updNotice', query: {noticeId: id, content: content, order: order === '重要' ? 1 : 2}})
    },
    deleteUser (id) {
      api.deleteNotice({
        token: this.$store.state.token,
        noticeId: id
      }).then((res) => {
        if (res.code === 342) {
          this.$store.commit('logout')
          this.$store.commit('tokenError')
          this.$router.push({path: '/login'})
        } else if (res.code < 300) {
          this.pageChange()
        } else {
          ElMessage.error(res.message)
        }
      })
    },
    handleSearch () {
      this.pageChange()
      // console.log(`current page: ${val}`)
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.pageChange()
      // console.log(`current page: ${val}`)
    },
    pageChange () {
      api.aGetAllNotice({
        content: this.page.search,
        token: this.$store.state.token,
        pageSize: this.page.pageSize,
        currentPage: this.page.currentPage
      }).then((res) => {
        if (res.code === 342) {
          this.$store.commit('logout')
          this.$store.commit('tokenError')
          this.$router.push({path: '/login'})
        } else if (res.code < 300) {
          this.userList = res.data.noticeList
          this.userList.forEach((item) => {
            item.order = item.order === 1 ? '重要' : '普通'
          })
          this.page.pageSize = res.data.pageSize
          this.page.currentPage = res.data.currentPage
          this.page.num = res.data.pageNum * res.data.pageSize
          // console.log(this.page)
        } else {
          ElMessage.error(res.message)
        }
      })
    }
  },
  mounted () {
    this.pageChange()
  }
}
</script>

<style scoped>

</style>
